package newcoder_exp.justPractise;

import java.util.*;

import org.junit.Test;

public class NC116 {

    /* 
        字符串解读
        如12 -> ab l
    */
    public class Solution {
        public int solve(String nums) {
            int len;
            if (nums == null || (len = nums.length()) == 0) return 0;
            char[] cs = nums.toCharArray();
            int[] f = new int[len]; //以i为尾的字符串解码数量
            f[0] = cs[0] == '0' ? 0 : 1;
            f[1] = f[0] + (check(cs[0], cs[1]) ? 1 : 0);
            for (int i = 2; i < len; i++) {
                if (cs[i] == '0') {
                    if (cs[i - 1] == '0') 
                        return 0; //没法翻译
                    else if (cs[i - 1] == '1' || cs[i - 1] == '2') {
                        f[i] = f[i - 2];
                    } else {
                        return 0;
                    }
                } else {
                    if (check(cs[i - 1], cs[i])) {
                        f[i] = f[i - 2] + f[i - 1];
                    } else {
                        f[i] = f[i - 1];
                    }
                }
            }
            return f[len - 1];
        }

        public boolean check(char c1, char c2) {
            if (c1 == '1' || c1 == '2' && c2 <= '6') return true;
            else return false;
        }


        // public int solve(String nums) {
        //     int len;
        //     if (nums == null || (len = nums.length()) == 0) 
        //         return 0;

        // }

        // public int dfs(char[] cs, int idx, int cur) {
        //     if (idx == cs.length) {
        //         return cur;
        //     }

        // }

    }

    @Test
    public void test() {
        String nums = "72416145196211821232022471311148103136128331523141061051992231223";
        
        Solution s = new Solution();
        int res = s.solve(nums);
        System.out.println(res);
    }
}
